package com.heima.article.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.article.mapper.ApArticleConfigMapper;
import com.heima.article.mapper.ApArticleContentMapper;
import com.heima.article.mapper.ApArticleMapper;
import com.heima.article.service.ApArticleService;
import com.heima.article.service.ArticleFreemarkerService;
import com.heima.common.constants.ArticleConstants;
import com.heima.model.article.dto.ArticleDto;
import com.heima.model.article.dto.ArticleHomeDto;
import com.heima.model.article.pojo.ApArticle;
import com.heima.model.article.pojo.ApArticleConfig;
import com.heima.model.article.pojo.ApArticleContent;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
@Transactional
@Slf4j
public class ApArticleServiceImpl extends ServiceImpl<ApArticleMapper, ApArticle> implements ApArticleService {

    @Autowired
    private ApArticleMapper apArticleMapper;

    @Autowired
    private ApArticleContentMapper apArticleContentMapper;

    @Autowired
    private ApArticleConfigMapper apArticleConfigMapper;

    @Autowired
    private ArticleFreemarkerService articleFreemarkerService;

    // 每页显示的最大条数
    private static final int MAX_PAGE_SIZE = 50;

    // 每页显示的默认条数
    private static final int PAGE_SIZE = 10;

    /**
     * 加载文章列表
     * @param type
     * @param articleHomeDto
     * @return
     */
    public ResponseResult load(Short type, ArticleHomeDto articleHomeDto) {
        // 效验参数
        Integer size = articleHomeDto.getSize();
        if (size == null || size == 0){
            size = PAGE_SIZE;
        }
        size = Math.min(size, MAX_PAGE_SIZE);
        articleHomeDto.setSize(size);
        // 类型参数检验
        if(!type.equals(ArticleConstants.LOADTYPE_LOAD_MORE) && !type.equals(ArticleConstants.LOADTYPE_LOAD_NEW)){
            type = ArticleConstants.LOADTYPE_LOAD_MORE;
        }
        // 文章频道校验
        if (StringUtils.isEmpty(articleHomeDto.getTag())){
            articleHomeDto.setTag(ArticleConstants.DEFAULT_TAG);
        }
        // 时间校验
        if (articleHomeDto.getMinBehotTime() == null){
            articleHomeDto.setMinBehotTime(new Date());
        }
        if (articleHomeDto.getMaxBehotTime() == null){
            articleHomeDto.setMaxBehotTime(new Date());
        }
        // 查询数据
        List<ApArticle> apArticles = apArticleMapper.loadApArticleList(type, articleHomeDto);
        return ResponseResult.okResult(apArticles);
    }

    /**
     * 保存APP端相关文章
     * @param articleDto
     * @return
     */
    @Transactional
    public ResponseResult saveArticle(ArticleDto articleDto) {
        // 效验参数
        if (articleDto == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        // 属性拷贝
        ApArticle apArticle = new ApArticle();
        BeanUtils.copyProperties(articleDto, apArticle);
        // 判断是否存在ID
        if (articleDto.getId() == null){
            // 不存在ID，则说明是第一次审核，进行文章同步
            //1、ap_article添加数据
            this.save(apArticle);
            //2、ap_article_Content添加数据
            ApArticleContent apArticleContent = new ApArticleContent();
            apArticleContent.setArticleId(apArticle.getId());
            apArticleContent.setContent(articleDto.getContent());
            apArticleContentMapper.insert(apArticleContent);
            //3、ap_article_config表中添加数据
            ApArticleConfig articleConfig = new ApArticleConfig();
            articleConfig.setArticleId(apArticle.getId());
            articleConfig.setIsComment(true);
            articleConfig.setIsForward(true);
            articleConfig.setIsDelete(false);
            articleConfig.setIsDown(false);
            apArticleConfigMapper.insert(articleConfig);
        }else {
            //说明以前已经同步过，现在做更新
            //1、更新ap_article
            this.updateById(apArticle);
            //2、更新内容
            LambdaQueryWrapper<ApArticleContent> wrapper = new LambdaQueryWrapper<>();
            ApArticleContent content = apArticleContentMapper.selectOne(wrapper.eq(ApArticleContent::getArticleId, articleDto.getId()));
            content.setContent(articleDto.getContent());
            apArticleContentMapper.updateById(content);
        }
        // 生成静态页面
        articleFreemarkerService.buildArticleToMinIO(apArticle, articleDto.getContent());
        return ResponseResult.okResult(apArticle.getId());
    }
}
